package com.aluvfy.jdbc;

import com.aluvfy.jdbc.utils.DbUtils;

import java.sql.*;

public class JDBCTest07 {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stm = null;
        ResultSet rs = null;
        try {
            conn = DbUtils.getConnection();
            stm = conn.createStatement();

            String sql = "INSERT INTO t_product(name, price, create_time) VALUES('小米酥', 1.0, '2024-02-04')";
            int count = stm.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);
            System.out.println("传入了"  + count + "条数据");
            rs = stm.getGeneratedKeys();
            if (rs.next()){
                Long id = rs.getLong(1);
                System.out.println("新增的主键值是：" + id);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            DbUtils.close(conn, stm, rs);
        }
    }
}
